package com.wn.common.util;

import com.wn.common.entity.Coordinate;

/**
 * @BelongsProject: shared_parking
 * @BelongsPackage: com.wn.common.util
 * @Author: 廖刚
 * @CreateTime: 2020-06-23 22:21
 * @Description:
 */
public class CoordinateUtil {

    private static double EARTH_RADIUS = 6378.137;

    private static double rad(double d) {
        return d * Math.PI / 180.0;
    }

    /**
     * 通过经纬度获取距离(单位：米)
     * @param lng1 经度1
     * @param lat1 纬度1
     * @param lng2 经度2
     * @param lat2 纬度2
     * @return
     */
    private static double getDistance(double lng1, double lat1, double lng2,
                                     double lat2) {
        double radLat1 = rad(lat1);
        double radLat2 = rad(lat2);
        double a = radLat1 - radLat2;
        double b = rad(lng1) - rad(lng2);
        double s = 2 * Math.asin(Math.sqrt(Math.pow(Math.sin(a / 2), 2)
                + Math.cos(radLat1) * Math.cos(radLat2)
                * Math.pow(Math.sin(b / 2), 2)));
        s = s * EARTH_RADIUS;
        s = Math.round(s * 10000d) / 10000d;
        s = s*1000;
        return s;
    }

  /*  public static void main(String[] args){
        //114.43285,30.462144
        *//*double lng1 = 114.43285;
        double lat1 = 30.462144;
        double lng2 = 114.431877;
        double lat2 = 30.461969;*//*

        double distance = getDistance(lng1,lat1,lng2,lat2);
        System.out.println("distanc:"+distance+"米");
    }*/
    /**
     * 计算两个坐标之间的距离，单位米
     */
    public static Double distanceByCoodinates(Coordinate coordinate1,Coordinate coordinate2){
        Double lng1 = Double.parseDouble(coordinate1.getLng());
        Double lat1 = Double.parseDouble(coordinate1.getLat());
        Double lng2 = Double.parseDouble(coordinate2.getLng());
        Double lat2 = Double.parseDouble(coordinate2.getLat());
        double distance = getDistance(lng1,lat1,lng2,lat2);
        return distance;
    }
}
